/**
 *
 * 版权所有 © cybersky.com
 *
 */
package cn.cybertron.primus.report.custom.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;

import cn.cybertron.primus.base.ChartType;
import cn.cybertron.primus.report.cache.ReportCacheManager;
import cn.cybertron.primus.report.data.ReportCacheSimple;
import cn.cybertron.primus.report.data.ZReportItem;
import cn.cybertron.primus.report.entity.ReportCache;
import cn.cybertron.primus.report.entity.ReportChart;
import cn.cybertron.primus.report.entity.ReportObject;

/**
 * 统计报表自定义助手实现类
 * 
 * @author ch
 *
 */
@IocBean(name="dataReportCustomHelper", singleton=true)
public class StatisticsReportCustomHelper extends AbstractReportCustomHelper {

	/**
	 * 处理每步所需数据，保存到request中
	 */ 
	@Override
	public void prepareData(int step, ReportObject report, HttpServletRequest request, Ioc ioc) {
		switch(step) {
			case 1: prepareBasicInfo(report, request, ioc); break;
			case 2: prepareDataSource(report, request, ioc); break;
			case 3: prepareDataFields(report, request, ioc); break;
			case 4: prepareChart(report, request, ioc); break;
		}
	}

	/**
	 * 准备数据源
	 */ 
	private void prepareDataSource(ReportObject report, HttpServletRequest request, Ioc ioc) {
		List<ReportCacheSimple> caches = getSourceCaches();
		request.setAttribute("caches", caches);
		
		request.getSession().removeAttribute(SESSION_KEY_FILTER);
		if(report != null) {
			// 设置选中中间表
			for(ReportCacheSimple cache: caches) {
				if(cache.getUuid().equals(report.getCacheid())) {
					request.setAttribute("selectedCache", cache);
					break;
				}
			}

			// 设置过滤条件
			if(report.getStrategy() != null){
				/*ReportQuery query = report.getQuery();
				if(query instanceof AbstractDBReportQuery){// 判断是否为中间表查询器
					
				}*/
				request.getSession().setAttribute(SESSION_KEY_FILTER, report.getStrategy());
			}
		}
	}

	/**
	 * 准备数据列
	 * @param report
	 * @param request
	 * @param ioc
	 */
	private void prepareDataFields(ReportObject report, HttpServletRequest request, Ioc ioc) {
		List<ZReportItem> fields = new ArrayList<ZReportItem>();
		
		// 选中数据源中字段
		if(report != null) {
			ReportCache cache = ReportCacheManager.me().getCacheByUuid(report.getCacheid());
			// 设置分组字段
			Vector<ZReportItem> groupitems = cache.getGroupitems();
			fields.addAll(groupitems);
			/*for(ZReportItem item: groupitems) {
				fields.add(item.getField());
			}*/
			// 设置统计字段
			Vector<ZReportItem> statitems = cache.getStatiitems();
			fields.addAll(statitems);
			/*for(ZReportItem item: statitems) {
				fields.add(item.getField());
			}*/
		}
		
		request.setAttribute("fields", fields);
	}

	/**
	 * 设置统计图类型
	 */ 
	private void prepareChart(ReportObject report, HttpServletRequest request, Ioc ioc) {
		if(report != null) {
			List<ReportChart> charts = report.getCharts();
			request.setAttribute("charts", charts);
		}
		request.setAttribute("chartTypes", Lang.list(ChartType.values()));
	}

	@Override
	public Map<String, Object> prepareQueryParams(Ioc ioc, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> prepareChartParams(Ioc ioc, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setQuery(ReportObject report, String jsonQuery, Map<String, Object> params) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getTimeColumnName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getQueryStr(ReportObject report) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareChart(List<ChartType> chartTypes, List<String> groupItems, List<String> statItems,
			ReportObject report) {
		// TODO Auto-generated method stub
		
	}

}
